package com.handinfo.android.uicontrols.controls;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.utils.Tools;

/**
 * 页码控件
 * @author handinfo
 */
public class DWPaging extends DWControl
{
	// 暂时先写单页模式
	public static int ONEPAGE_STYLE = 0; // 只显示当前页码风格
	public static int ALLPAGE_STYLE = 1; // 显示多页风格

	private int m_allPage = 0; // 总页数
	private int m_nowPage = 0;// 当前页数

	private int m_pageStyle = ONEPAGE_STYLE; // 显示风格，默认为单页模式
	private DWGraphics m_g;
	private String text; // 中间的

	private String StrLeft = " ◀ ";
	private String StrRight = " ▶ ";

	DWPaging(int allPage, int w, int h)
	{

		if (allPage > 0) {
			m_allPage = allPage;
			m_nowPage = 1;
		}
		setShowWideHigh(w, h);
	}

	DWPaging(int allPage, int x, int y, int w, int h)
	{
		if (allPage > 0) {
			m_allPage = allPage;
			m_nowPage = 1;
		}
		setPoint(x, y);
		setShowWideHigh(w, h);
	}

	/**
	 * 设置控件风格
	 * 
	 * @param style
	 */
	public void setPageStyle(int style)
	{
		if (style == ONEPAGE_STYLE || style == ALLPAGE_STYLE) {
			m_pageStyle = style;
		}
	}

	public void render(DWGraphics g)
	{
		m_g = g;
		g.setColor(-1);
		g.setClip(m_show_x, m_show_y, m_show_w, m_show_h);
		paintPageNum();
	}

	/**
	 * 绘制显示样式
	 */
	public void paintPageNum()
	{
		if (m_pageStyle == ONEPAGE_STYLE)
			text = m_nowPage + "/" + m_allPage;
		else if (m_pageStyle == ALLPAGE_STYLE)
			text = "";

		m_g.drawString(StrLeft + text + StrRight, Tools.BLACK, m_show_x, m_show_y,
				Tools.HCENTER_VCENTER);
	}

	public void logic()
	{

	}

	public boolean doClick(float pointX, float pointY)
	{
		if (m_pageStyle == ONEPAGE_STYLE) {
			if (pointX < (m_show_x + m_show_w) / 2) // 前一页
			{
				if (m_nowPage > 1) {
					m_nowPage--;
					paintPageNum();
				}
			}
			else if (pointX > (m_show_x + m_show_w) / 2) // 后一页
			{
				if (m_nowPage < m_allPage) {
					m_nowPage++;
					paintPageNum();
				}
			}
		}
		return false;
	}

}
